
import { post } from "./request.js"
import { postFile } from "./axios.js"
import store from "../store"
import ElementUI from 'element-ui';

function OssLoad(file) {
	console.log(file)
	return new Promise((resolve, reject) => {
		post("/front/user/generateSignedUrl").then((result) => {
			let ossData = result.data.obj
			console.log(ossData)
			let random = Date.parse(new Date()) + store.state.user.name + store.state.user.id
            let fileName = file.name
            let formData = new FormData()
            formData.append("key", ossData.dir +"/" + random + fileName)
            formData.append("policy", ossData.policy)
            formData.append("OSSAccessKeyId", ossData.oSSAccessKeyId)
            formData.append("success_action_status", "200")
            formData.append("signature", ossData.signature)
            formData.append("host", ossData.host)
			formData.append("file", file)
            postFile(ossData.host, formData).then(()=>{
				let data = `${ossData.host}/${ossData.dir}/${random}${fileName}`
				resolve(data)
            }).catch(()=>{
				ElementUI.Message.info("上传失败")
			})
		})

	})
}

export default OssLoad